<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>采购订单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="text" name="id" id="id" style="display: none"
           class="layui-input">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-inline">
                <input type="text" name="orderNo" id="orderNo"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">签约日期</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="signDate"
                       id="signDate">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">订单有效起始日期</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="validStartDate"
                       id="validStartDate">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">订单有效结束日期</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="validEndDate"
                       id="validEndDate">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">签约地点</label>
            <div class="layui-input-inline">
                <input type="text" name="signLocation" id="signLocation"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">交期计划方式</label>
            <div class="layui-input-inline">
                <select name="deliveryPlanType" id="deliveryPlanType" lay-search="">
                    <option value="">请选择</option>
                    <option value="FIXED_DATE">固定日期</option>
                    <option value="BY_ORDER_RATIO">按订单比例</option>
                    <option value="INSTALLMENT">分期</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">交期条款</label>
            <div class="layui-input-inline">
                <input type="text" name="deliveryTerms" id="deliveryTerms"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">预计交货日期</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="expectedDeliveryDate"
                       id="expectedDeliveryDate">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">提货期限开始</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="pickupPeriodStart"
                       id="pickupPeriodStart">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">提货期限结束</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="pickupPeriodEnd"
                       id="pickupPeriodEnd">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">付款期限</label>
            <div class="layui-input-inline">
                <input readonly="readonly" type="text" class="layui-input" name="paymentTerms"
                       id="paymentTerms">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline">
                <select name="supId" id="supId" lay-search="">
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">业务来源</label>
            <div class="layui-input-inline">
                <select name="bizSource" id="bizSource" lay-search="">
                    <option value="">请选择</option>
                    <option value="INTERNAL">内部</option>
                    <option value="EXTERNAL">外部</option>
                    <option value="FRAMEWORK">框架</option>
                    <option value="OTHER">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">业务来源说明</label>
            <div class="layui-input-inline">
                <input type="text" name="bizSourceDesc" id="bizSourceDesc"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">来源单据号</label>
            <div class="layui-input-inline">
                <input type="text" name="sourceDocNo" id="sourceDocNo"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">增值税税率</label>
            <div class="layui-input-inline">
                <input type="number" name="taxRate" id="taxRate"
                       value="" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save-quotation">
                保存订单
            </button>
        </div>
    </div>
</div>

<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    let purchaseOrderId = $.common.getUrlParam("purchaseOrderId");
    let user = $.common.getUserInfo();
    layui.use(['form', 'laydate', 'element', 'layer'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;

        // 日期选择器
        laydate.render({
            elem: '#signDate',
            type: 'date'
        });
        laydate.render({
            elem: '#validStartDate',
            type: 'date'
        });
        laydate.render({
            elem: '#validEndDate',
            type: 'date'
        });
        laydate.render({
            elem: '#expectedDeliveryDate',
            type: 'date'
        });
        laydate.render({
            elem: '#pickupPeriodStart',
            type: 'date'
        });
        laydate.render({
            elem: '#pickupPeriodEnd',
            type: 'date'
        });
        laydate.render({
            elem: '#paymentTerms',
            type: 'datetime'
        });

        // 供应商下拉框
        let supplierCallback = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#supId').append(new Option('请选择', '', true));
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.supplierName, item.id);
                        $('#supId').append(option);
                    });
                } else {
                    $.common.showErrorMessage(result.message);
                    return false;
                }
            }
        };
        $.xAjax('/supplier/select', 'POST', JSON.stringify({}), supplierCallback);

        // 初始化下拉框
        form.render("select");

        // 监听提交
        form.on('submit(save-quotation)', function (data) {
            let url = "/purchaseOrder/saveOrUpdate";
            let callback = {
                onSuccess: function (result) {
                    if (result.code == '0') {
                        layer.msg('保存成功', {icon: 1});
                        // 可以在这里跳转到列表页面或关闭弹窗
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        if (iframeIndex) {
                            parent.layer.close(iframeIndex);
                            parent.location.reload();
                        }
                    } else {
                        $.common.showErrorMessage(result.message);
                        return false;
                    }
                }
            };
            $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
            return false;
        });

        // 如果是编辑模式，加载数据
        if (purchaseOrderId) {
            let loadCallback = {
                onSuccess: function (result) {
                    if (result.code == '0') {
                        let data = result.data;
                        for (let key in data) {
                            $("#" + key).val(data[key]);
                        }
                        form.render("select");
                    } else {
                        $.common.showErrorMessage(result.message);
                        return false;
                    }
                }
            };
            $.xAjax('/purchaseOrder/getById/' + purchaseOrderId, 'POST', JSON.stringify({}), loadCallback);
        }
    });
</script>
</body>
</html>
